﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Entry;
using Cofe.Core.Filter;
using Cofe.Core.Property;

namespace Cofe.Data.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class DBEntryListRegistrar : EntryListRegistrarBase
    {
        #region Constructor

        public DBEntryListRegistrar()
        {
        }

        #endregion

        #region Methods


        protected override T register<T>(IPropertyProvider entryListPP)
        {
            var entryListPH = CofeServices.EntryConstructor.ConstructPropertyHost(entryListPP);

            using (var r = CofeDBServices.RepositoryFactory.CreateRepository())
            {
                r.AddOrUpdate(true, false, entryListPH);
                r.SaveChanges();
            }

            var entryList = CofeServices.EntryConstructor.ConstructEntry<T>(entryListPH);
            return entryList;
        }

        public override IPropertyHost Find(string name)
        {
            using (var r = CofeDBServices.RepositoryFactory.CreateRepository())
            {
                var retVal = r.FindAll(FilterCollections.FromParseName(name)).FirstOrDefault();
                return retVal;
            }
        }

        public override void Remove(string name)
        {
            using (var r = CofeDBServices.RepositoryFactory.CreateRepository())
            {
                r.Remove(FilterCollections.FromParseName(name));
                r.SaveChanges();
            }
        }

        #endregion

        #region Data
        

        #endregion

        #region Public Properties

        #endregion

    }
}
